package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.ProMajor;
import com.wolves.career.entity.ProProfessional;
import com.wolves.career.entity.vo.MajorQuery;
import com.wolves.career.service.ProMajorService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 专业 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-31
 */
@RestController
@RequestMapping("/career/major")
@CrossOrigin
public class ProMajorController {

    @Autowired
    private ProMajorService majorService;

    @GetMapping("/{id}")
    public R getMajor(@PathVariable String id){
        return R.ok().data("major",majorService.getById(id));
    }

    @DeleteMapping("/{id}")
    public R removeMajor(@PathVariable String id){
        majorService.removeById(id);
        return R.ok();
    }

    @PutMapping("/major")
    public R addMajor(@RequestBody ProMajor major){
        majorService.save(major);
        return R.ok();
    }

    @PostMapping("/major")
    public R updateMajor(@RequestBody ProMajor major){
        majorService.updateById(major);
        return R.ok();
    }

    @PostMapping("/list/{pageNo}/{limit}")
    public R listMajor(@PathVariable long pageNo,
                       @PathVariable long limit,
                      @RequestBody(required = false) ProMajor major){
        QueryWrapper<ProMajor> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(major.getMajorName())) {
            queryWrapper.like("major_name",major.getMajorName());
        }
        Page<ProMajor> page = new Page<>(pageNo,limit);
        IPage<ProMajor> majorPage = majorService.page(page, queryWrapper);
        return R.ok().data("records",majorPage.getRecords()).data("total",majorPage.getTotal());
    }


    @GetMapping("/getMajorProfession/{professionId}")
    public R getMajorByProfession(@PathVariable String professionId){
        List<ProMajor> list = majorService.getMajorByProfessionId(professionId);
        return R.ok().data("list",list);
    }

    @PostMapping("/getMajorProfession/{pageNo}/{limit}")
    public R getMajorByVo(@RequestBody MajorQuery majorQuery, 
                          @PathVariable long pageNo,
                          @PathVariable long limit){
        List<ProMajor> list = majorService.getMajorByVo(majorQuery);
        // TODO: 2021/11/1  
        return R.ok();
    }

}

